<?php

//获取周边
function getAround($location, $keywords, $radius = 3000){
	$res = '';
	$key = "ae6668dfc165182e74d97b3f017ee575";
	$url = "https://restapi.amap.com/v3/place/around?key=$key&location=$location&keywords=$keywords&types=$keywords&radius=$radius&offset=20&page=1&extensions=";
	$json = file_get_contents($url);
	$data = json_decode($json,true);
	if(!empty($data['pois'])){
		$arr = array();
		foreach ($data['pois'] as $v) {
			if(empty($v['address'])){
				continue;
			}
			$temp['distance'] = !empty($v['distance']) ? $v['distance'] : '';
			$temp['name'] = !empty($v['name']) ? $v['name'] : '';
			$temp['address'] = !empty($v['address']) ? $v['address'] : '';
			$temp['location'] = !empty($v['location']) ? $v['location'] : '';
			// $temp['business_area'] = !empty($v['business_area']) ? $v['business_area'] : '';
			// $temp['photos'] = !empty($v['photos']) ? $v['photos'] : '';
			$arr[] = $temp;
		}
		$res = json_encode($arr, JSON_UNESCAPED_UNICODE);
	}
	$res = str_replace("'", '', $res);
	return $res;
}

function getAroundGongjiao($location, $radius = 1000){
	$key = "ae6668dfc165182e74d97b3f017ee575";
	$url = "https://restapi.amap.com/v3/place/around?key=$key&location=$location&keywords=公交&types=公交&radius=$radius&offset=50&page=1";
	$json = file_get_contents($url);
	$data = json_decode($json,true);
	$res = null;
	$str = '';
	if(!empty($data['pois'])){
		$res['gjz_count'] = 0;
		foreach ($data['pois'] as $v) {
			$res['gjz_count'] += 1;
			if(!empty($v['address'])){
				$str .= $v['address'].';';
			}
		}
		$str = rtrim($str,';');
		$arr = explode(';', $str);
    	$arr = array_unique($arr);
		$res['gjx_count'] = count($arr);
	}
	return $res;
}

function getAroundDitie($location, $radius = 1000){
	$key = "ae6668dfc165182e74d97b3f017ee575";
	$url = "https://restapi.amap.com/v3/place/around?key=$key&location=$location&keywords=地铁&types=地铁&radius=$radius&offset=1&page=1";
	$json = file_get_contents($url);
	$data = json_decode($json,true);
	$res = null;
	if(!empty($data['pois'])){
		$res['dt_name'] = $data['pois'][0]['name'];
		$res['dt_distance'] = $data['pois'][0]['distance'];
	}
	return $res;
}

function getAroundTingche($location, $radius = 2000){
	$key = "ae6668dfc165182e74d97b3f017ee575";
	$url = "https://restapi.amap.com/v3/place/around?key=$key&location=$location&keywords=停车场&types=停车场&radius=$radius&offset=1&page=1";
	$json = file_get_contents($url);
	$data = json_decode($json,true);
	$res = null;
	if(!empty($data['count'])){
		$res = $data['count'];
	}
	return $res;
}

function getAroundBiaozhi($location, $radius = 1000){
	$key = "ae6668dfc165182e74d97b3f017ee575";
	$url = "https://restapi.amap.com/v3/place/around?key=$key&location=$location&types=学校;医院;商场;写字楼&radius=$radius&offset=20&page=1";
	$json = file_get_contents($url);
	$data = json_decode($json,true);
	$res = null;
	if(!empty($data['pois'])){
		foreach ($data['pois'] as $v) {
			$res .= "{$v['name']}({$v['distance']}米)、";
		}
		$res = rtrim($res,'、');
	}
	return $res;
}

function getAddress($location,$name,$radius=1000){
	$key = "ae6668dfc165182e74d97b3f017ee575";
	$url = "https://restapi.amap.com/v3/place/around?key=$key&location=$location&keywords=$name&radius=$radius&offset=1&page=1";
	$json = file_get_contents($url);
	$data = json_decode($json,true);
	$res = '';
	if(!empty($data['pois'])){
		$res = $data['pois'][0]['address'];
	}
	return $res;
}

function getJieshao($location,$name,$address){
	$str = $gongjiao = $ditie = $tingche = $biaozhi = '';
	$gj = getAroundGongjiao($location);
	if(!empty($gj)){
		$gongjiao = "附近共有{$gj['gjz_count']}个公交站点{$gj['gjx_count']}个公交线路可以到达。";
	}
	$dt = getAroundDitie($location);
	if(!empty($dt)){
		$ditie = "除此之外您也可以乘坐地铁到{$dt['dt_name']}下车步行{$dt['dt_distance']}米也可到达{$name}。";
	}
	$tc = getAroundTingche($location);
	if(!empty($tc)){
		$tingche = "如果您是驾车的话，{$name}所在附近有{$tc}个停车场可供您停车。";
	}
	$bz = getAroundBiaozhi($location);
	if(!empty($bz)){
		$biaozhi = "附近的标志性建筑有{$bz}等。";
	}
	$str = "{$name}位于{$address}，{$gongjiao}{$ditie}{$tingche}{$biaozhi}";
	return $str;
}

function get_around_data($json){
	$json = htmlspecialchars_decode($json);
	$data = json_decode($json,true);
	return $data;
}

function get_bus($json){
    $str = '';
    $json = htmlspecialchars_decode($json);
    $data = json_decode($json,true);
    foreach ($data as $v) {
        $str .= $v['name'].'、';
    }
    $str = rtrim($str,'、');
    return $str;
}

function get_bus_line($json){
    $str = '';
    $json = htmlspecialchars_decode($json);
    $data = json_decode($json,true);
    foreach ($data as $v) {
        $str .= $v['address'].';';
    }
    $str = rtrim($str,';');
    $arr = explode(';', $str);
    $arr = array_unique($arr);
    $str = implode('、', $arr);
    return $str;
}

//获取封面图
function getLitpic($json,$uid){
	if(empty($json)){
		return '';
	}
    $data = json_decode($json,true);
	if(empty($data[0]['url'])){
		return '';
	}
	$url = $data[0]['url'];
	$file_name = $uid.'.jpg';
	$dir_name = DS.'uploads'.DS.'dianpu'.DS;
	$dir_root = app()->getRootPath().'public';
	if(!is_dir($dir_root.$dir_name)){
		mkdir($dir_root.$dir_name,0777,true);
	}

	try{
		ob_start();//打开输出
		readfile($url);//输出图片文件
		$img = ob_get_contents();//得到浏览器输出
		ob_end_clean();//清除输出并关闭
		// $size = strlen($img);//得到图片大小
	}catch(Exception $e){
		return '';
	}
	
	$fp = @fopen($dir_root.$dir_name.$file_name, "a");
	fwrite($fp, $img);//向当前目录写入图片文件，并重新命名
	fclose($fp);
	
	return str_replace(DS, '/', $dir_name).$file_name;
}

//获取天气API
function tianqiApi($city,$province=''){
	$appid = '13336234';
	$appsecret = 'qfvI81hl';
	$url = "https://v0.yiketianqi.com/api?unescape=1&version=v62&appid={$appid}&appsecret={$appsecret}&city={$city}&province={$province}";
	$json = file_get_contents($url);
	if(strpos($json,'errcode') !== false){
		return null;
	}
	return $json;
}

//获取首字母
function getFirst($str)
{
  if (empty($str)) {
    return '';
  }
  //取出参数字符串中的首个字符
  $temp_str = substr($str,0,1);
  if(ord($temp_str) > 127){
    $str = substr($str,0,3);
  }else{
    $str = $temp_str;
    $fchar = ord($str[0]);
    if ($fchar >= ord('A') && $fchar <= ord('z')){
      return strtoupper($temp_str);
    }else{
      return '';
    }
  }
  $s1 = iconv('UTF-8', 'gb2312//IGNORE', $str);
  if(empty($s1)){
    return '';
  }
  $s2 = iconv('gb2312', 'UTF-8', $s1);
  if(empty($s2)){
    return '';
  }
  $s = $s2 == $str ? $s1 : $str;
  $asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
  if ($asc >= -20319 && $asc <= -20284)
    return 'A';
  if ($asc >= -20283 && $asc <= -19776)
    return 'B';
  if ($asc >= -19775 && $asc <= -19219)
    return 'C';
  if ($asc >= -19218 && $asc <= -18711)
    return 'D';
  if ($asc >= -18710 && $asc <= -18527)
    return 'E';
  if ($asc >= -18526 && $asc <= -18240)
    return 'F';
  if ($asc >= -18239 && $asc <= -17923)
    return 'G';
  if ($asc >= -17922 && $asc <= -17418)
    return 'H';
  if ($asc >= -17417 && $asc <= -16475)
    return 'J';
  if ($asc >= -16474 && $asc <= -16213)
    return 'K';
  if ($asc >= -16212 && $asc <= -15641)
    return 'L';
  if ($asc >= -15640 && $asc <= -15166)
    return 'M';
  if ($asc >= -15165 && $asc <= -14923)
    return 'N';
  if ($asc >= -14922 && $asc <= -14915)
    return 'O';
  if ($asc >= -14914 && $asc <= -14631)
    return 'P';
  if ($asc >= -14630 && $asc <= -14150)
    return 'Q';
  if ($asc >= -14149 && $asc <= -14091)
    return 'R';
  if ($asc >= -14090 && $asc <= -13319)
    return 'S';
  if ($asc >= -13318 && $asc <= -12839)
    return 'T';
  if ($asc >= -12838 && $asc <= -12557)
    return 'W';
  if ($asc >= -12556 && $asc <= -11848)
    return 'X';
  if ($asc >= -11847 && $asc <= -11056)
    return 'Y';
  if ($asc >= -11055 && $asc <= -10247)
    return 'Z';
  return rare_words($asc);
}
//百家姓中的生僻字
function rare_words($asc=''){
  $rare_arr = array(
    -3652=>array('word'=>"窦",'first_char'=>'D'),
    -8503=>array('word'=>"奚",'first_char'=>'X'),
    -9286=>array('word'=>"酆",'first_char'=>'F'),
    -7761=>array('word'=>"岑",'first_char'=>'C'),
    -5128=>array('word'=>"滕",'first_char'=>'T'),
    -9479=>array('word'=>"邬",'first_char'=>'W'),
    -5456=>array('word'=>"臧",'first_char'=>'Z'),
    -7223=>array('word'=>"闵",'first_char'=>'M'),
    -2877=>array('word'=>"裘",'first_char'=>'Q'),
    -6191=>array('word'=>"缪",'first_char'=>'M'),
    -5414=>array('word'=>"贲",'first_char'=>'B'),
    -4102=>array('word'=>"嵇",'first_char'=>'J'),
    -8969=>array('word'=>"荀",'first_char'=>'X'),
    -4938=>array('word'=>"於",'first_char'=>'Y'),
    -9017=>array('word'=>"芮",'first_char'=>'R'),
    -2848=>array('word'=>"羿",'first_char'=>'Y'),
    -9477=>array('word'=>"邴",'first_char'=>'B'),
    -9485=>array('word'=>"隗",'first_char'=>'K'),
    -6731=>array('word'=>"宓",'first_char'=>'M'),
    -9299=>array('word'=>"郗",'first_char'=>'X'),
    -5905=>array('word'=>"栾",'first_char'=>'L'),
    -4393=>array('word'=>"钭",'first_char'=>'T'),
    -9300=>array('word'=>"郜",'first_char'=>'G'),
    -8706=>array('word'=>"蔺",'first_char'=>'L'),
    -3613=>array('word'=>"胥",'first_char'=>'X'),
    -8777=>array('word'=>"莘",'first_char'=>'S'),
    -6708=>array('word'=>"逄",'first_char'=>'P'),
    -9302=>array('word'=>"郦",'first_char'=>'L'),
    -5965=>array('word'=>"璩",'first_char'=>'Q'),
    -6745=>array('word'=>"濮",'first_char'=>'P'),
    -4888=>array('word'=>"扈",'first_char'=>'H'),
    -9309=>array('word'=>"郏",'first_char'=>'J'),
    -5428=>array('word'=>"晏",'first_char'=>'Y'),
    -2849=>array('word'=>"暨",'first_char'=>'J'),
    -7206=>array('word'=>"阙",'first_char'=>'Q'),
    -4945=>array('word'=>"殳",'first_char'=>'S'),
    -9753=>array('word'=>"夔",'first_char'=>'K'),
    -10041=>array('word'=>"厍",'first_char'=>'S'),
    -5429=>array('word'=>"晁",'first_char'=>'C'),
    -2396=>array('word'=>"訾",'first_char'=>'Z'),
    -7205=>array('word'=>"阚",'first_char'=>'K'),
    -10049=>array('word'=>"乜",'first_char'=>'N'),
    -10015=>array('word'=>"蒯",'first_char'=>'K'),
    -3133=>array('word'=>"竺",'first_char'=>'Z'),
    -6698=>array('word'=>"逯",'first_char'=>'L'),
    -9799=>array('word'=>"俟",'first_char'=>'Q'),
    -6749=>array('word'=>"澹",'first_char'=>'T'),
    -7220=>array('word'=>"闾",'first_char'=>'L'),
    -10047=>array('word'=>"亓",'first_char'=>'Q'),
    -10005=>array('word'=>"仉",'first_char'=>'Z'),
    -3417=>array('word'=>"颛",'first_char'=>'Z'),
    -6431=>array('word'=>"驷",'first_char'=>'S'),
    -7226=>array('word'=>"闫",'first_char'=>'Y'),
    -9293=>array('word'=>"鄢",'first_char'=>'Y'),
    -6205=>array('word'=>"缑",'first_char'=>'G'),
    -9764=>array('word'=>"佘",'first_char'=>'S'),
    -9818=>array('word'=>"佴",'first_char'=>'N'),
    -9509=>array('word'=>"谯",'first_char'=>'Q'),
    -3122=>array('word'=>"笪",'first_char'=>'D'),
    -9823=>array('word'=>"佟",'first_char'=>'T'),
  );
  if(array_key_exists($asc, $rare_arr) && $rare_arr[$asc]['first_char']){
    return $rare_arr[$asc]['first_char'] ;
  }else{
    return '';
  }
}